package com.tangye.massageshop.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tangye.massageshop.repository.UserRepository;
import com.tangye.massageshop.entity.UserEntity;
import com.tangye.massageshop.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserRepositoryImpl implements UserRepository {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserEntity selectByName(String userName) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserEntity> selectUsersPage(UserEntity userEntity) {
        QueryWrapper<UserEntity> qw = new QueryWrapper<>();

        if (Objects.nonNull(userEntity.getCompanyId())) {
            qw.eq("company_id", userEntity.getCompanyId());
        }
        if (Objects.nonNull(userEntity.getShopId())) {
            qw.eq("shop_id", userEntity.getShopId());
        }
        if (StringUtils.isNotBlank(userEntity.getUserName())) {
            qw.like("user_name", userEntity.getUserName());
        }
        if (StringUtils.isNotBlank(userEntity.getCompanyName())) {
            qw.like("company_name", userEntity.getCompanyName());
        }

        Page<UserEntity> page = new Page<>(userEntity.getPageNum(), userEntity.getPageSize());
        Page<UserEntity> resultPage = userMapper.selectPage(page, qw);
        return resultPage.getRecords();
    }
}
